package com.ruoyi.workbench.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.workbench.domain.WbContract;
import com.ruoyi.workbench.service.IWbContractService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 合同Controller
 *
 * @author ruoyi
 * @date 2020-10-10
 */
@Controller
@RequestMapping("/workbench/contract")
public class WbContractController extends BaseController {
    private final String prefix = "workbench/contract";

    private final IWbContractService wbContractService;

    public WbContractController(IWbContractService wbContractService) {
        this.wbContractService = wbContractService;
    }

    @RequiresPermissions("workbench:contract:view")
    @GetMapping()
    public String contract() {
        return prefix + "/contract";
    }

    /**
     * 查询合同列表
     */
    @RequiresPermissions("workbench:contract:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WbContract wbContract) {
        startPage();
        List<WbContract> list = wbContractService.selectWbContractList(wbContract);
        return getDataTable(list);
    }

    /**
     * 导出合同列表
     */
    @RequiresPermissions("workbench:contract:export")
    @Log(title = "合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WbContract wbContract) {
        List<WbContract> list = wbContractService.selectWbContractList(wbContract);
        ExcelUtil<WbContract> util = new ExcelUtil<>(WbContract.class);
        return util.exportExcel(list, "contract", ShiroUtils.getLoginName());
    }

    /**
     * 新增合同
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存合同
     */
    @RequiresPermissions("workbench:contract:add")
    @Log(title = "合同", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WbContract wbContract) {
        return toAjax(wbContractService.insertWbContract(wbContract));
    }

    /**
     * 修改合同
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        WbContract wbContract = wbContractService.selectWbContractById(id);
        mmap.put("wbContract", wbContract);
        return prefix + "/edit";
    }

    /**
     * 修改保存合同
     */
    @RequiresPermissions("workbench:contract:edit")
    @Log(title = "合同", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WbContract wbContract) {
        return toAjax(wbContractService.updateWbContract(wbContract));
    }

    /**
     * 删除合同
     */
    @RequiresPermissions("workbench:contract:remove")
    @Log(title = "合同", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(wbContractService.deleteWbContractByIds(ids));
    }

    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<WbContract> util = new ExcelUtil<>(WbContract.class);
        List<WbContract> userList = util.importExcel(file.getInputStream());
        String operateName = ShiroUtils.getSysUser().getLoginName();
        String message = wbContractService.importUser(userList, operateName);
        return AjaxResult.success(message);
    }
}
